package com.example.yuv2rbg;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;

public class MainActivity extends AppCompatActivity {


    private ViewGroup mRootLayer;

    private Button mBtnYuv420p, mBtnNV12, mBtnNV21;

    private SurfaceView mSurfaceView;

    private NativeYUV2RGB mNativeYUV2RGB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }
    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        setupView();
        setupAsserts();
    }

    private void setupAsserts() {
        String[] targetFiles = {"yuv420p.yuv", "nv12.yuv", "nv21.yuv"};


        new Thread(new Runnable() {
            @Override
            public void run() {

                for (int  i = 0;i < targetFiles.length;i++){
                    releaseAsserts(targetFiles[i],getApplicationContext());
                }
            }
        }).start();


        onReleaseComplete();
    }

    private void setupView() {
        mRootLayer = (ViewGroup) findViewById(R.id.native_yub2rgb_root_layer);
        mBtnYuv420p = (Button) findViewById(R.id.btn_yuv420p);
        mBtnNV12 = (Button) findViewById(R.id.btn_nv12);
        mBtnNV21 = (Button) findViewById(R.id.btn_nv21);

        mSurfaceView = new SurfaceView(this);
        mRootLayer.addView(mSurfaceView);
        mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                mNativeYUV2RGB = new NativeYUV2RGB();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        });
    }

    public void onYUV420PToRGB24(View view) {
        File file = new File(getExternalFilesDir(null), "yuv420p.yuv");
        mNativeYUV2RGB.yuv2rgb(file.getAbsolutePath(), NativeYUV2RGB.Type.YUV420P_TO_RGB24, 510, 510, mSurfaceView.getHolder().getSurface());
    }

    public void onNV12ToRGB24(View view) {
        File file = new File(getExternalFilesDir(null), "nv12.yuv");
        mNativeYUV2RGB.yuv2rgb(file.getAbsolutePath(), NativeYUV2RGB.Type.NV12_TO_RGB24, 510, 510, mSurfaceView.getHolder().getSurface());
    }

    public void onNV21ToRGB24(View view) {
        File file = new File(getExternalFilesDir(null), "nv21.yuv");
        mNativeYUV2RGB.yuv2rgb(file.getAbsolutePath(), NativeYUV2RGB.Type.NV21_TO_RGB24, 510, 510, mSurfaceView.getHolder().getSurface());
    }


    public void onReleaseComplete() {
        mBtnYuv420p.setEnabled(true);
        mBtnNV12.setEnabled(true);
        mBtnNV21.setEnabled(true);
    }


    /**
     * 释放Asserts目录下指定文件
     * @param fileName
     * @return
     */
    private String releaseAsserts(String fileName,Context context) {
        InputStream inputStream = null;
        FileOutputStream fos = null;
        try {
            Context appContext = context;
            if (appContext != null) {
                inputStream = appContext.getAssets().open(fileName);
                File targetFile = new File(appContext.getExternalFilesDir(null), fileName);
                fos = new FileOutputStream(targetFile);
                int length;
                byte[] buffer = new byte[8 * 1024];
                while ((length = inputStream.read(buffer)) != -1) {
                    fos.write(buffer, 0, length);
                    fos.flush();
                }
                return targetFile.getAbsolutePath();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            ioClose(fos);
            ioClose(inputStream);
        }
        return null;
    }
    private void ioClose(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }





}